import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class demo10 {
    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<Integer> queue=new LinkedBlockingQueue<>(10);

        Thread customer=new Thread(()->{
            while (true){
                try {
                    int value = queue.take();
                    System.out.println("消费元素："+value);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },"消费者");

        customer.start();

        Thread producer=new Thread(()->{
            Random random=new Random();
            while (true){
                    try {
                        int num= random.nextInt(1000);
                        queue.put(num);
                        System.out.println("生产元素："+num);
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
        },"生产者");
        producer.start();
        customer.join();
        producer.join();
    }
}
